home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 51 / Amiga Format CD51 (2000-03-10)(Future Publishing)(GB)[!][issue 2000-04].iso / -in_the_mag- / workbench / term_4.8 / extras / source / term-source.lha / IFF.c < prev    next >
C/C++ Source or Header  |  1997-10-03  |  5KB  |  319 lines

  1. /*
  2. **    IFF.c
  3. **
  4. **    Interchange file format I/O support routines
  5. **
  6. **    Copyright © 1990-1997 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16. /****************************************************************************/
  17.  
  18.     /* CloseIFFClip(struct IFFHandle *Handle):
  19.      *
  20.      *    Closes the clipboard opened by OpenIFFClip.
  21.      */
  22.  
  23. BOOL
  24. CloseIFFClip(struct IFFHandle *Handle)
  25. {
  26.     if(Handle != NULL)
  27.     {
  28.         CloseIFF(Handle);
  29.         CloseClipboard((struct ClipboardHandle *)Handle->iff_Stream);
  30.  
  31.         FreeIFF(Handle);
  32.     }
  33.  
  34.     return(TRUE);
  35. }
  36.  
  37.     /* OpenIFFClip(LONG Unit,LONG Mode):
  38.      *
  39.      *    Open the clipboard for reading or writing. This
  40.      *    routine handles all the initizalizations and
  41.      *    allocation and returns an IFFHandle ready
  42.      *    to be used.
  43.      */
  44.  
  45. struct IFFHandle *
  46. OpenIFFClip(
  47.     LONG Unit,
  48.     LONG Mode)
  49. {
  50.     struct IFFHandle *Handle;
  51.     LONG Error;
  52.  
  53.     ASSERT(Mode == MODE_NEWFILE || Mode == MODE_OLDFILE);
  54.  
  55.     Handle = AllocIFF();
  56.     if(Handle != NULL)
  57.     {
  58.         Handle->iff_Stream = (ULONG)OpenClipboard(Unit);
  59.         if(Handle->iff_Stream != NULL)
  60.         {
  61.             LONG type;
  62.  
  63.             InitIFFasClip(Handle);
  64.  
  65.             if(Mode == MODE_NEWFILE)
  66.                 type = IFFF_WRITE;
  67.             else
  68.                 type = IFFF_READ;
  69.  
  70.             Error = OpenIFF(Handle,type);
  71.             if(Error != OK)
  72.                 CloseClipboard((struct ClipboardHandle *)Handle->iff_Stream);
  73.         }
  74.         else
  75.         {
  76.             Error = ERROR_NO_FREE_STORE;
  77.         }
  78.  
  79.         if(Error != OK)
  80.         {
  81.             FreeIFF(Handle);
  82.             Handle = NULL;
  83.         }
  84.     }
  85.     else
  86.     {
  87.         Error = ERROR_NO_FREE_STORE;
  88.     }
  89.  
  90.     if(Error != OK)
  91.         SetIoErr(Error);
  92.  
  93.     return(Handle);
  94. }
  95.  
  96. /****************************************************************************/
  97.  
  98.     /* CloseIFFStream(struct IFFHandle *Handle):
  99.      *
  100.      *    Closes an IFF file opened by OpenIFFStream.
  101.      */
  102.  
  103. BOOL
  104. CloseIFFStream(struct IFFHandle *Handle)
  105. {
  106.     BOOL Result;
  107.  
  108.     if(Handle != NULL)
  109.     {
  110.         CloseIFF(Handle);
  111.         Result = Close((BPTR)Handle->iff_Stream);
  112.  
  113.         FreeIFF(Handle);
  114.     }
  115.     else
  116.     {
  117.         Result = TRUE;
  118.     }
  119.  
  120.     return(Result);
  121. }
  122.  
  123.     /* OpenIFFStream(STRPTR Name,LONG Mode):
  124.      *
  125.      *    Open an IFF file for reading or writing. This
  126.      *    routine handles all the initizalizations and
  127.      *    allocation and returns an IFFHandle ready
  128.      *    to be used.
  129.      */
  130.  
  131. struct IFFHandle *
  132. OpenIFFStream(
  133.     const STRPTR    Name,
  134.     LONG            Mode)
  135. {
  136.     struct IFFHandle *Handle;
  137.     LONG Error;
  138.  
  139.     ASSERT(Name != NULL && (Mode == MODE_NEWFILE || Mode == MODE_OLDFILE));
  140.  
  141.     Handle = AllocIFF();
  142.     if(Handle != NULL)
  143.     {
  144.         Handle->iff_Stream = (ULONG)Open((STRPTR)Name,Mode);
  145.         if(Handle->iff_Stream != NULL)
  146.         {
  147.             LONG type;
  148.  
  149.             InitIFFasDOS(Handle);
  150.  
  151.             if(Mode == MODE_NEWFILE)
  152.                 type = IFFF_WRITE;
  153.             else
  154.                 type = IFFF_READ;
  155.  
  156.             Error = OpenIFF(Handle,type);
  157.             if(Error != OK)
  158.             {
  159.                 Close((BPTR)Handle->iff_Stream);
  160.  
  161.                 if(Mode == MODE_NEWFILE)
  162.                     DeleteFile((STRPTR)Name);
  163.             }
  164.         }
  165.         else
  166.         {
  167.             Error = IoErr();
  168.         }
  169.  
  170.         if(Error != OK)
  171.         {
  172.             FreeIFF(Handle);
  173.             Handle = NULL;
  174.         }
  175.     }
  176.     else
  177.     {
  178.         Error = ERROR_NO_FREE_STORE;
  179.     }
  180.  
  181.     if(Error != OK)
  182.         SetIoErr(Error);
  183.  
  184.     return(Handle);
  185. }
  186.  
  187. /****************************************************************************/
  188.  
  189. BOOL
  190. ReadIFFBytes(
  191.     struct IFFHandle *    handle,
  192.     APTR                buffer,
  193.     LONG                numBytes)
  194. {
  195.     BOOL result;
  196.     LONG total;
  197.  
  198.     ASSERT(handle != NULL && buffer != NULL && numBytes > 0);
  199.  
  200.     total = ReadChunkBytes(handle,buffer,numBytes);
  201.     if(total < 0)
  202.     {
  203.         SetIoErr(total);
  204.         result = FALSE;
  205.     }
  206.     else
  207.     {
  208.         result = TRUE;
  209.     }
  210.  
  211.     return(result);
  212. }
  213.  
  214. BOOL
  215. ReadIFFRecords(
  216.     struct IFFHandle *    handle,
  217.     APTR                buffer,
  218.     LONG                bytesPerRecord,
  219.     LONG                numRecords)
  220. {
  221.     BOOL result;
  222.     LONG total;
  223.  
  224.     ASSERT(handle != NULL && buffer != NULL && bytesPerRecord > 0 && numRecords > 0);
  225.  
  226.     total = ReadChunkRecords(handle,buffer,bytesPerRecord,numRecords);
  227.     if(total < 0)
  228.     {
  229.         SetIoErr(total);
  230.         result = FALSE;
  231.     }
  232.     else
  233.     {
  234.         result = TRUE;
  235.     }
  236.  
  237.     return(result);
  238. }
  239.  
  240. BOOL
  241. WriteIFFBytes(
  242.     struct IFFHandle *    handle,
  243.     const APTR            buffer,
  244.     LONG                numBytes)
  245. {
  246.     BOOL result;
  247.     LONG total;
  248.  
  249.     ASSERT(handle != NULL && buffer != NULL && numBytes > 0);
  250.  
  251.     total = WriteChunkBytes(handle,(APTR)buffer,numBytes);
  252.     if(total < 0)
  253.     {
  254.         SetIoErr(total);
  255.         result = FALSE;
  256.     }
  257.     else
  258.     {
  259.         result = TRUE;
  260.     }
  261.  
  262.     return(result);
  263. }
  264.  
  265. BOOL
  266. WriteIFFRecords(
  267.     struct IFFHandle *    handle,
  268.     const APTR            buffer,
  269.     LONG                bytesPerRecord,
  270.     LONG                numRecords)
  271. {
  272.     BOOL result;
  273.     LONG total;
  274.  
  275.     ASSERT(handle != NULL && buffer != NULL && bytesPerRecord > 0 && numRecords > 0);
  276.  
  277.     total = WriteChunkRecords(handle,(APTR)buffer,bytesPerRecord,numRecords);
  278.     if(total < 0)
  279.     {
  280.         SetIoErr(total);
  281.         result = FALSE;
  282.     }
  283.     else
  284.     {
  285.         result = TRUE;
  286.     }
  287.  
  288.     return(result);
  289. }
  290.  
  291. /****************************************************************************/
  292.  
  293. LONG
  294. AddIFFChunkBytes(
  295.     struct IFFHandle *    handle,
  296.     LONG                chunkID,
  297.     const APTR            buffer,
  298.     LONG                numBytes)
  299. {
  300.     LONG status;
  301.  
  302.     ASSERT(handle != NULL && buffer != NULL && numBytes > 0);
  303.  
  304.     status = PushChunk(handle,0,chunkID,numBytes);
  305.     if(status == OK)
  306.     {
  307.         LONG total;
  308.  
  309.         total = WriteChunkBytes(handle,(APTR)buffer,numBytes);
  310.         if(total < 0)
  311.             status = total;
  312.     }
  313.  
  314.     if(status == OK)
  315.         status = PopChunk(handle);
  316.  
  317.     return(status);
  318. }
  319.